import type { G6GraphEvent } from '@antv/g6';
export default function (G6: any) {
	// Register a custom behavior: click two end nodes to add an edge
	G6.registerBehavior('click-add-edge', {
		// Set the events and the corresponding responsing function for this behavior
		getEvents() {
			return {
				'node:click': 'onClick', // The event is canvas:click, the responsing function is onClick
				mousemove: 'onMousemove', // The event is mousemove, the responsing function is onMousemove
				click: 'onEdgeClick', // The event is edge:click, the responsing function is onEdgeClick
			};
		},
		// 鼠标移动事件
		onMousemove(ev: G6GraphEvent) {
			// const this = this;
			if (this.addingEdge && this.edge) {
				const point = { x: ev.x, y: ev.y };
				// 更新边的坐标为鼠标当前的坐标
				this.graph.updateItem(this.edge, {
					target: point,
				});
			}
		},
		// 点击事件，点击canvas空白区域取消当前不完整的边
		onEdgeClick(ev: G6GraphEvent) {
			// const this = this;
			const currentEdge = ev.item;
			if (this.addingEdge && this.edge === currentEdge) {
				this.graph.removeItem(this.edge);
				this.edge = null;
				this.addingEdge = false;
			}
		},
	});
}
